package org.jeecg.modules.courseplan.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.classroom.entity.FacClassroom;
import org.jeecg.modules.common.utils.StringUtil;
import org.jeecg.modules.courseplan.entity.CouCoursePlan;
import org.jeecg.modules.courseplan.service.ICouCoursePlanService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.courseplan.vo.CouCoursePlanVo;
import org.jeecg.modules.relation.relbuildingclassroom.entity.RelBuildingClassroom;
import org.jeecg.modules.relation.relbuildingclassroom.vo.RelBuildingClassroomVo;
import org.jeecg.modules.relation.relclssubtearoom.vo.RelClassSubjectTeacherVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: cou_course_plan
 * @author: baochangrong
 * @Date:   2022-02-17
 * @Version: V1.0
 */
@Api(tags="cou_course_plan")
@RestController
@RequestMapping("/courseplan/couCoursePlan")
@Slf4j
public class CouCoursePlanController extends JeecgController<CouCoursePlan, ICouCoursePlanService> {
	@Autowired
	private ICouCoursePlanService couCoursePlanService;
	
	/**
	 * 分页列表查询
	 *
	 * @param couCoursePlan
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-分页列表查询")
	@ApiOperation(value="cou_course_plan-分页列表查询", notes="cou_course_plan-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CouCoursePlan couCoursePlan,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		couCoursePlan.setTenantId(tenantId);
		QueryWrapper<CouCoursePlan> queryWrapper = QueryGenerator.initQueryWrapper(couCoursePlan, req.getParameterMap());
		Page<CouCoursePlan> page = new Page<CouCoursePlan>(pageNo, pageSize);
		IPage<CouCoursePlan> pageList = couCoursePlanService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页查询全部 voList
	  */
	 @AutoLog(value = "cou_course_plan-分页列表查询")
	 @ApiOperation(value="cou_course_plan-分页列表查询", notes="cou_course_plan-分页列表查询")
	 @GetMapping(value = "/listAll")
	 public Result<?> listAll(CouCoursePlanVo couCoursePlanVo,
							  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							  HttpServletRequest req) {
		 String tenantId = req.getHeader("tenant-id");
		 Page<CouCoursePlanVo> page = new Page<CouCoursePlanVo>(pageNo, pageSize);
		 IPage<CouCoursePlanVo> pageList = couCoursePlanService.listAll(page,tenantId);
		 return Result.OK(pageList);
	 }

	 /**
	  * 教学计划相关 queryList
	  */
	 @AutoLog(value = "cou_course_plan-列表查询")
	 @ApiOperation(value="cou_course_plan-列表查询", notes="cou_course_plan-列表查询")
	 @GetMapping(value = "/queryList")
	 public Result<?> queryList(CouCoursePlan couCoursePlan,
							  HttpServletRequest req) {
		 String tenantId = req.getHeader("tenant-id");
		 List<CouCoursePlan> couCoursePlanList = couCoursePlanService.queryList(tenantId);
		 return Result.OK(couCoursePlanList);
	 }

	 /**
	 *   添加
	 *
	 * @param couCoursePlan
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-添加")
	@ApiOperation(value="cou_course_plan-添加", notes="cou_course_plan-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CouCoursePlan couCoursePlan,HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		couCoursePlan.setTenantId(tenantId);
		couCoursePlanService.save(couCoursePlan);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param couCoursePlan
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-编辑")
	@ApiOperation(value="cou_course_plan-编辑", notes="cou_course_plan-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CouCoursePlan couCoursePlan,HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		couCoursePlan.setTenantId(tenantId);
		couCoursePlanService.updateById(couCoursePlan);
		return Result.OK("编辑成功!");
	}
	/** 逻辑删除 */
	 @PutMapping(value = "/planIzdelete")
	 public Result<?> planIzdelete(@RequestBody CouCoursePlan couCoursePlan,HttpServletRequest req) {
		 String tenantId = req.getHeader("tenant-id");
		 couCoursePlan.setId(couCoursePlan.getId());
		 couCoursePlan.setIzDelete(1);
		 couCoursePlan.setTenantId(tenantId);
		 boolean _Delete = couCoursePlanService.planIzdelete(couCoursePlan);
		 if (_Delete){
			 return Result.OK("删除成功(逻辑删除)");
		 }else {
			 return Result.error("删除失败");
		 }
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-通过id删除")
	@ApiOperation(value="cou_course_plan-通过id删除", notes="cou_course_plan-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		couCoursePlanService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-批量删除")
	@ApiOperation(value="cou_course_plan-批量删除", notes="cou_course_plan-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.couCoursePlanService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cou_course_plan-通过id查询")
	@ApiOperation(value="cou_course_plan-通过id查询", notes="cou_course_plan-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CouCoursePlan couCoursePlan = couCoursePlanService.getById(id);
		if(couCoursePlan==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(couCoursePlan);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param couCoursePlan
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CouCoursePlan couCoursePlan) {
        return super.exportXls(request, couCoursePlan, CouCoursePlan.class, "cou_course_plan");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CouCoursePlan.class);
    }


	 /**
	  * 升级
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "insertUpgradeCouCoursePlan-升级")
	 @ApiOperation(value = "insertUpgradeCouCoursePlan-升级", notes = "insertUpgradeCouCoursePlan-升级")
	 @GetMapping(value = "/insertUpgradeCouCoursePlan")
	 public Result<?> insertUpgradeCouCoursePlan(@RequestParam(name = "schoolYearId", required = true) String schoolYearId,
												   @RequestParam(name = "semesterId", required = true) String semesterId,
												   @RequestParam(name = "gradeId", required = true) String gradeId) {

		 Assert.hasText(gradeId, "年级Id没有传递，无法升年级");
		 Assert.hasText(schoolYearId, "目标学年没有传递，请选择");
		 Assert.hasText(semesterId, "目标学期没有传递，请选择");
		 CouCoursePlanVo couCoursePlanVo = new CouCoursePlanVo();
		 couCoursePlanVo.setSchoolYearId(schoolYearId);
		 couCoursePlanVo.setSemesterId(semesterId);
		 if (!StringUtil.isEmpty(gradeId)) {
			 couCoursePlanVo.setGradeId(gradeId);
		 }
		 if(couCoursePlanService.insertUpgradeCouCoursePlan(couCoursePlanVo) > 0){
			 return Result.OK("教学计划、计划详情、计划下达升级成功！");
		 }else {
			 return Result.OK("教学计划相关升级失败！");
		 }
	 }

}
